package com.turbo.boot.crawler.dao.dal;

import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.turbo.boot.crawler.dao.mapper.CrawlerResultMapper;
import com.turbo.framework.dal.TurboBaseDal;
import com.turbo.starter.mybatis.TurboDalImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述
 *
 * @author wuyn
 * @since 2020/4/11
 */
@Repository
public class CrawlerResultDal extends TurboDalImpl<CrawlerResultMapper, CrawlerResultEntity> implements TurboBaseDal<CrawlerResultEntity> {


    public int insert(CrawlerResultEntity entity) {
        CrawlerResultEntity query = new CrawlerResultEntity();
        query.setCollectedAt(entity.getCollectedAt());
        query.setQuotaCode(entity.getQuotaCode());
        query.setGroupId(entity.getGroupId());
        CrawlerResultEntity exists = selectOne(query);
        if (exists == null) {
            return super.insert(entity);
        } else {
            entity.setId(exists.getId());
            return update(entity);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public int insertBatch(List<CrawlerResultEntity> batch) {
        int total = 0;
        for (CrawlerResultEntity item : batch) {
            total += insert(item);
        }
        return total;
    }
}
